package com.jeepig.web.controller.evaluation;

import com.jeepig.common.annotation.Log;
import com.jeepig.common.core.controller.BaseController;
import com.jeepig.common.core.domain.AjaxResult;
import com.jeepig.common.core.page.TableDataInfo;
import com.jeepig.common.enums.BusinessType;
import com.jeepig.common.utils.poi.ExcelUtil;
import com.jeepig.evaluation.domain.Member;
import com.jeepig.evaluation.service.IComEvaluationService;
import com.jeepig.evaluation.service.IMemberService;
import com.jeepig.evaluation.service.ISelfEvaluationService;
import com.jeepig.evaluation.vo.OverviewEvalutionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 待评价人员Controller
 *
 * @author guoxb
 * @date 2025-11-01
 */
@RestController
@RequestMapping("/evaluation/member")
public class MemberController extends BaseController
{
    @Autowired
    private IMemberService memberService;

    @Autowired
    private ISelfEvaluationService selfEvaluationService;

    @Autowired
    private IComEvaluationService comEvaluationService;

    /**
     * 查询待评价人员列表
     */
    @PreAuthorize("@ss.hasPermi('evaluation:member:list')")
    @GetMapping("/list")
    public TableDataInfo list(Member member)
    {
        startPage();
        List<Member> list = memberService.selectMemberList(member);
        return getDataTable(list);
    }

    /**
     * 导出待评价人员列表
     */
    @PreAuthorize("@ss.hasPermi('evaluation:member:export')")
    @Log(title = "待评价人员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Member member)
    {
        List<Member> list = memberService.selectMemberList(member);
        ExcelUtil<Member> util = new ExcelUtil<Member>(Member.class);
        util.exportExcel(response, list, "待评价人员数据");
    }

    /**
     * 获取待评价人员评价项
     *
     * @param memberId
     * @return
     */
    @GetMapping("/getEvaluationItems")
    public AjaxResult getEvaluationItems(Long memberId, Long evaluateId){
        Map<String,Object> result = new HashMap<>();
        result.put("member", memberService.selectMemberById(String.valueOf(memberId)));
        result.put("comEvaluation", comEvaluationService.selectComEvaluationByMemberId(memberId, evaluateId));
        result.put("selfEvaluation", selfEvaluationService.selectSelfEvaluationByMemberId(memberId, evaluateId));
        result.put("evaluationItems", memberService.selectDeptEvalutionVos(memberId, evaluateId));
        return AjaxResult.success(result);
    }

    /**
     * 获取待评价人员详细信息
     */
    @PreAuthorize("@ss.hasPermi('evaluation:member:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(memberService.selectMemberById(id));
    }

    /**
     * 待评价人员总览
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('evaluation:member:query')")
    @GetMapping("/overview/{id}")
    public AjaxResult overview(@PathVariable("id") String id){
        Map<String, Object> params = new HashMap<>();
        Member member = memberService.selectMemberById(id);
        //获取综合评价
        List<OverviewEvalutionVo> overviewEvalutionVos = memberService.selectOverviewEvalution(Long.parseLong(id));
        params.put("member", member);
        params.put("overviewEvalutionVos", overviewEvalutionVos);
        return AjaxResult.success(params);
    }

    /**
     * 新增待评价人员
     */
    @PreAuthorize("@ss.hasPermi('evaluation:member:add')")
    @Log(title = "待评价人员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Member member)
    {
        return toAjax(memberService.insertMember(member));
    }

    /**
     * 更新综合评分
     *
     * @param memberId
     * @param score
     * @return
     */
    @Log(title = "待评价人员", businessType = BusinessType.UPDATE)
    @GetMapping("/updateComEvaluation/{memberId}")
    public AjaxResult updateComEvaluation(@PathVariable("memberId") Long memberId,
                                          String score, String content){
        Member member = memberService.selectMemberById(String.valueOf(memberId));
        if(member != null){
            member.setScore( score);
            member.setExt1(content);
        }
        memberService.updateMember(member);
        return AjaxResult.success("评价成功！");
    }

    /**
     * 修改待评价人员
     */
    @PreAuthorize("@ss.hasPermi('evaluation:member:edit')")
    @Log(title = "待评价人员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Member member)
    {
        return toAjax(memberService.updateMember(member));
    }

    /**
     * 删除待评价人员
     */
    @PreAuthorize("@ss.hasPermi('evaluation:member:remove')")
    @Log(title = "待评价人员", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(memberService.deleteMemberByIds(ids));
    }
}
